package com.txunda.zhuohired.fgt.manage;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.L;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshBase;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshListView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.manage.my.ManageResumeDateAdapter;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.manage.my.ManageResumeDataBean;
import com.txunda.zhuohired.http.pst.UserPst;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/14 13:39
 * 功能描述：面试日程
 * 联系方式：jiahuanchen@126.com
 */

public class ManageResumeDateFgt extends BaseFgt {

    private int status_type = 0;
    /**
     * PullToRefreshListView
     */
    @ViewInject(R.id.lv_manage_resume_date)
    private PullToRefreshListView lv_manage_resume_date;
    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;
    private ManageResumeDateAdapter manageResumeDateAdapter;
    private List<ManageResumeDataBean.DataBean> manageDataList;
    private String token ;
    private UserPst userPst;
    private int page = 1;


    @Override
    protected void immersionInit() {

    }
    /**
     * 每次fragment可见时
     * <p/>
     * 请求接口
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    initHttp();
                }
            }, 100);
        } else {
            // 不可见时不执行操作
        }
    }
    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_manage_resume_date;
    }

    @Override
    protected void initialized() {
        userPst = new UserPst(this);
        token = application.getUserInfo().get("data");


    }
    private void initHttp(){
        userPst.InterviewPlan(token,String.valueOf(status_type),String.valueOf(page));
    }


    @Override
    protected void requestData() {
        initHttp();
    }

    private void initAdapter() {
        manageResumeDateAdapter = new ManageResumeDateAdapter(getActivity(),manageDataList);
        lv_manage_resume_date.setAdapter(manageResumeDateAdapter);
    }

    public static ManageResumeDateFgt getFgt(int type) {
        ManageResumeDateFgt manageResumeDateFgt = new ManageResumeDateFgt();
        manageResumeDateFgt.status_type = type;
        return manageResumeDateFgt;
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if(requestUrl.contains("InterviewPlan")){
            L.e("InterviewPlan",jsonStr);
            lv_manage_resume_date.onRefreshComplete();
            ManageResumeDataBean manageResumeDataBean = GsonUtil.GsonToBean(jsonStr,ManageResumeDataBean.class);

            manageDataList = manageResumeDataBean.getData();
            if(page == 1){
                if(manageDataList.size()==0){
                    noData(2);
                }else{
                    noData(1);
                    initAdapter();
                }
            }else{
                if(manageResumeDataBean.getData().size()==0){
                    showToast("已经最后一页了，没有数据了");
                }else{
                    List myEventList = manageResumeDataBean.getData();
                    manageDataList.addAll(myEventList);
                    manageResumeDateAdapter.notifyDataSetChanged();
                }
            }
            initAdapter();
        }

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lv_manage_resume_date.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initHttp();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page ++;
                initHttp();
            }
        });
    }


    /**
     * 空数据
     *
     * @param type
     */
    private void noData(int type){
        if(type==1){
            lv_manage_resume_date.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        }else{
            lv_manage_resume_date.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        }
    }
}
